package mvpfoot.dao;

import android.content.Context;

import com.j256.ormlite.dao.Dao;

import java.sql.SQLException;
import java.util.List;

import mvpfoot.db.MySqlite;
import mvpfoot.entity.GoodsBean;

/**
 * Created by Chen qin lang on 2017/1/9.
 */
public class UserDao {
    public Dao<GoodsBean,Integer> goodsDao;
    public UserDao(Context context) {
        MySqlite sqlite = MySqlite.getMySqlite(context);
        goodsDao =  sqlite.getDao(GoodsBean.class);
    }
    public boolean addGoods(GoodsBean goodsBean){
        try {
            int i = goodsDao.create(goodsBean);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
    public List<GoodsBean> query(){
        try {
            List<GoodsBean> goodsBeen = goodsDao.queryForAll();
            return goodsBeen;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }

    public GoodsBean queryforDate(String date){
        try {
            List<GoodsBean> dates = goodsDao.queryBuilder().where().eq("date", date).query();
            if (date!=null){
                GoodsBean bean = dates.get(dates.size() - 1);
                return  bean;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }
}
